﻿@model IEnumerable<NetSurveillance.WebApp.DataServices.Models.Case>
@{
    ViewBag.Title = string.Format(Resources.MenuResource.SetupWizard_CaseAndTask, ViewBag.Step ?? string.Empty);
    Layout = "~/Views/Shared/_SetupWizardLayout.cshtml";
}
@using (Html.BeginForm())
{
    <table id="tbCaseAndTask">
        <tr>
            <td>@Resources.ViewResource.SetupWizard_CaseAndTask_CaseLabel</td>
            <td>
                @Html.DropDownList("caseId", new SelectList(Model, "Id", "Name"), new { id = "slCase", @size = 10 })
            </td>
            <td valign="top">
                <dl class="filemanage_right_dl">
                    <dd id="btnAddCase"><a href="@Url.Action("SaveCase", "Case")">@Resources.Resource.Button_AddCase</a></dd>      
                </dl>
            </td>
        </tr>
        <tr>
            <td>@Resources.ViewResource.SetupWizard_CaseAndTask_TaskLabel</td>
            <td colspan="2">
                @Html.TextBox("taskName", null, new { placeholder = Resources.ViewResource.SetupWizard_CaseAndTask_TaskTip })
            </td>
        </tr>
    </table>
    <div class="buttonsContain controlBottom">
        <div class="rightContain">
            <input id="btnPreSubmit" type="button" value="@Resources.Resource.Button_PreStep"/>
            <input id="btnSubmit" type="submit" value="@Resources.Resource.Button_NextStep"/>
        </div>
    </div>
    <input type="hidden" id="hiPreSubmit" name="preSubmit"/>
}
@section css
{
    <style type="text/css">
        #tbCaseAndTask {
            width: 600px;
            margin: 0 auto;
        }
        #tbCaseAndTask tr td {
            margin: 10px 0;
            padding: 10px 0;
        }
        .editor-entry .editor-label
        {
            width: 100px;
        }
        #slCase,#taskName {
            width: 300px;
        }
    </style>
}
@section javascript
{
    <script type="text/javascript">
        $(function() {
            $('#btnAddCase').click(function () {
                addCase('@Resources.Resource.Button_AddCase');
                return false;
            });
            $('#taskName').placeholder();
            $('#btnPreSubmit').click(function () {
                $('#hiPreSubmit').val('PreStep');
                $('form')[0].submit();
            });
        });

        function addCase(title, caseId) {
            if (!caseId) caseId = -1;
            var opts = {
                url: '@Url.Action("SaveCase", "Case")?caseId=' + caseId + '&__randomDate=' + (new Date()).getTime(),
                title: title,
                height: 230,
                width: 600,
                buttons: {
                    '@Resources.Resource.Button_Save': saveCase,
                    '@Resources.Resource.Button_Cancel': $.closeWindow
                },
                loadedFun: function () {
                    $.validator.unobtrusive.parse($.globalIds.tempWindow);
                }
            };
            $.openWindow(opts);
        }

        function saveCase() {
            var $saveCaseForm = $($.globalIds.tempFormInWindow);
            if ($saveCaseForm.valid()) {
                var caseModel = {};
                caseModel.Name = $saveCaseForm.find('#Name').val();
                caseModel.Desc = $saveCaseForm.find('#Desc').val();
                $.ajax({
                    url: '@Url.Action("SaveCase","Case")',
                    type: 'post',
                    dataType: 'json',
                    cache: false,
                    data: caseModel,
                    success: function(result) {
                        if (result.Success && result.Expandos) {
                            $('#slCase').append('<option value="' + result.Expandos.Id + '">' + result.Expandos.Name + '</option>')
                                .val(result.Expandos.Id);
                            $.closeWindow();
                            return;
                        }
                        alert(result.Message);
                    }
                });
            }
            return;
        }
    </script>
}
